<?php

class Exam_Widget_ListPopularExamsController extends Engine_Content_Widget_Abstract
{
  public function indexAction()
  {
    // Should we consider views or comments popular?
    $popularType = $this->_getParam('popularType', 'view');
    if( !in_array($popularType, array('test', 'view','difficult')) ) {
      $popularType = 'view';
    }
    $this->view->popularType = $popularType;
    $this->view->popularCol = $popularCol = $popularType . '_count';

    // Get paginator
    if($popularType== 'difficult'){
        $table = Engine_Api::_()->getItemTable('exam');
        $select = $table->select()
          ->order('average_score ASC');
    }
    else{
        $table = Engine_Api::_()->getItemTable('exam');
        $select = $table->select()
          ->order($popularCol . ' DESC');        
    }

    $this->view->paginator = $paginator = Zend_Paginator::factory($select);

    // Set item count per page and current page number
    $paginator->setItemCountPerPage($this->_getParam('itemCountPerPage', 3));
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));

    // Hide if nothing to show
    if( $paginator->getTotalItemCount() <= 0 ) {
      return $this->setNoRender();
    }
  }
}
